using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebTools.UI.WebControls
{
	internal static class RolloverManager
	{
		internal const string ClientScriptKey = "WebTools.UI.Scripts.Rollover.js";

		internal static void RegisterClientScript(IRollover rollover, bool registerScript)
		{
			if ((rollover.Page != null) && registerScript)
			{
				if (!rollover.Page.ClientScript.IsClientScriptBlockRegistered(RolloverManager.ClientScriptKey))
				{
					rollover.Page.ClientScript.RegisterClientScriptResource(typeof(IRollover), RolloverManager.ClientScriptKey);
				}

				// PreLoad Script
				string preLoadKey = String.Concat(rollover.ID, "PreLoad");
				if (!rollover.Page.ClientScript.IsStartupScriptRegistered(preLoadKey))
				{
					string imgUrl = rollover.MouseOverImageUrl;
					if (!String.IsNullOrEmpty(imgUrl))
					{
						string script = String.Format(CultureInfo.InvariantCulture, "var __WebToolsRollover_{0} = WebToolsRollover_PreLoadImage('{1}');{2}",
													rollover.ID, rollover.ResolveClientUrl(imgUrl), Environment.NewLine);
						rollover.Page.ClientScript.RegisterStartupScript(typeof(IRollover), preLoadKey, script, true);
					}
				}
			}
		}

		internal static void AddAttributesToRender(HtmlTextWriter writer, IRollover rollover)
		{
			string script = String.Empty;

			// Caption
			string caption = rollover.Caption;

			// CaptionControlID
			string captionClientId = null;
			string captionControlId = rollover.CaptionControlID;
			if (!String.IsNullOrEmpty(captionControlId))
			{
				Control captionControl = FindControlRecursive(rollover.Page, captionControlId);
				if (captionControl != null)
				{
					captionClientId = captionControl.ClientID;
				}
			}

			// OnMouseOver
			if (rollover.EnableMouseOver)
			{
				string overImage = rollover.MouseOverImageUrl;
				if (!String.IsNullOrEmpty(overImage))
				{
					string imageClientId = rollover.ImageClientID;

					// TargetImageControlID
					string targetImageControlId = rollover.TargetImageControlID;
					if (!String.IsNullOrEmpty(targetImageControlId))
					{
						Control control = FindControlRecursive(rollover.Page, targetImageControlId);
						if (control != null)
						{
							imageClientId = control.ClientID;
						}
					}

					// function WebToolsRollover_ChangeImage(imageControlId, imageUrl, captionControlId, captionText) 
					script = String.Format(CultureInfo.InvariantCulture, "return WebToolsRollover_ChangeImage('{0}', '{1}', '{2}', '{3}');",
						imageClientId, rollover.ResolveClientUrl(overImage), captionClientId, caption);
					writer.AddAttribute("onmouseover", script);
				}
			}

			// OnMouseOut
			if (rollover.EnableMouseOut)
			{
				string outImage = rollover.MouseOutImageUrl;
				if (!String.IsNullOrEmpty(outImage))
				{
					// function WebToolsRollover_ChangeImage(imageControlId, imageUrl, captionControlId, captionText) 
					script = String.Format(CultureInfo.InvariantCulture, "return WebToolsRollover_ChangeImage('{0}', '{1}', '{2}', '{3}');",
						rollover.ImageClientID, rollover.ResolveClientUrl(outImage), captionClientId, caption);
					writer.AddAttribute("onmouseout", script);
				}
			}
		}

		internal static Control FindControlRecursive(Control parent, string id)
		{
			if ((!String.IsNullOrEmpty(parent.ID)) && parent.ID.Equals(id))
			{
				return parent;
			}
			foreach (Control control in parent.Controls)
			{
				Control retControl = FindControlRecursive(control, id);
				if (retControl != null)
				{
					return retControl;
				}
			}
			return null;
		}
	}
}
